package com.king.java.tools.ssh.impl;

import ch.ethz.ssh2.ChannelCondition;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import com.king.java.tools.ssh.AbstractSSHutils;
import com.king.java.tools.ssh.RemoteInfo;
import com.king.java.tools.ssh.SSHUtils;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 描述:
 *
 * @author : sujinchen
 * @date : 2020/10/21
 */
public class GanymedSSHUtils extends AbstractSSHutils implements SSHUtils {


    private static final int TIME_OUT = 1000 * 5 * 60;

    private Connection conn;



    /**
     * 连接主机
     * @param remoteInfo 远程主机信息
     * @return
     */
    @Override
    public String ssh(RemoteInfo remoteInfo) {
        Connection conn = new Connection(remoteInfo.getHost());
        Session session = null;
        String key = "";
        try {
            conn.connect();
            boolean connect = conn.authenticateWithPassword(remoteInfo.getUser(), remoteInfo.getPassword());
            if (connect) {
                // Open a new {@link Session} on this connection
                session = conn.openSession();
                key=getKey("Ganymed");
            } else {
                key = "failed";
            }
        } catch (IOException e) {
            e.printStackTrace();
            key = "exception";
        }
        setSSH(key, session);
        return key;
    }

    /**
     * 执行单条指令
     * @param key 连接主机的ID
     * @param command shell指令
     * @return
     */
    @Override
    public List<String> send(String key, String command) {
        List<String> resultLines = new ArrayList<>();
        Session session = null;
        InputStream stdOut = null;
        InputStream stdErr = null;
        String outStr = "";
        String outErr = "";
        int ret = -1;
        try {
             session = (Session) getSSH(key);
            // 执行一个远程命令
                session.execCommand(command);
                stdOut = new StreamGobbler(session.getStdout());
            stdErr = new StreamGobbler(session.getStderr());
            bufferRead(resultLines, stdOut);
            bufferRead(resultLines, stdErr);
                session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);
                ret = session.getExitStatus();
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            if (conn != null) {
                conn.close();
            }
            IOUtils.closeQuietly(stdOut);
            IOUtils.closeQuietly(stdErr);
            if (session != null) {
                session.close();
                removeSSH(key);
            }
        }
        System.out.println("连接状态："+ret);
        return resultLines;
    }

    @Override
    public void interaction(String key, InputStream in, OutputStream out) {

    }

    @Override
    public List<String> sends(String key, String[] commands) {
        return null;
    }


    public static void main(String args[]) throws Exception {
        SSHUtils executor = new GanymedSSHUtils();
        String ssh = executor.ssh(new RemoteInfo());
        String command = "source /home/droid/autoTestDir/go.sh";
        List<String> pwd = executor.send(ssh, command);
//        System.out.println(command+":\n"+pwd);
        // 执行myTest.sh 参数为java Know dummy
    }
}
